﻿#region Legal

// Jimmy Zimmerman
// Team Mongoose
//
// END USER LICENSE AGREEMENT
// IMPORTANT - READ THIS CAREFULLY:  This End User License Agreement is a legal agreement
// between you (either an individual, business entity, currently active identity of an
// individual with Multiple Personality Disorder, octopus overlord, or otherworldly entity),
// and Team Mongoose, for the enclosed, components.
//
// By reading this document and/or installing this product, you agree to be bound by the
// terms of this EULA.
//
// Team Mongoose owns all copyright, trade secret, trademark, trade wars,
// patent, portent, and potential rights to these components.  Team Mongoose
// grants you the right to deploy the enclosed components.
//
// If you agree to the terms of this EULA, a license to use these components is granted to you.
//
// If you should happen to benefit from the enclosed components, then you are legally and
// contractually bound to thank us for it. Send your regards to jimmyzimms@hotmail.com.
//
// OTHER RIGHTS AND LIMITATONS
// You may not reverse-engineer, decompile, decode, disassemble, psycho-analyze, or otherwise
// attempt to find hidden meanings between the lines of this EULA; unless, of course, you should
// happen to find some, and they are funny.
//
// You may not translate this EULA or any part of the components into Ancient Sumerian.
//
// THESE COMPONENTS ARE PROVIDED “AS-IS” WITHOUT WARRANTY OF ANY KIND. ANY USE OF THE COMPONENTS
// CONTAINED IS AT YOUR OWN RISK. TEAM MONGOOSE DISCLAIM ALL WARRANTIES, EITHER
// EXPRESS OR IMPLIED, WITH RESPECT TO THE ACCURRACY AND CORRECTNESS OF THE COMPONENTS CONTAINED
// HEREIN. TEAM MONGOOSE DOES NOT WARRANT THAT THE COMPONENTS ARE FLAWLESS.
//
// REDISTRIBUTION AND USE IN SOURCE AND BINARY FORMS, WITH OR WITHOUT MODIFICATION, ARE PERMITTED
// PROVIDED THAT THE FOLLOWING CONDITIONS ARE MET:
// * REDISTRIBUTIONS OF SOURCE CODE MUST RETAIN THE ABOVE COPYRIGHT NOTICE
// * REDISTRIBUTIONS IN BINARY FORM MUST NOTE THE USE OF THE COMPONENT IN DOCUMENTATION AND/OR
//   OTHER MATERIALS PROVIDED WITH THE DISTRIBUTION.
// * NEITHER THE NAME OF TEAM MONGOOSE MAY BE USED TO ENDORES OR PROMOTE PRODUCTS
//   DERIVED FROM THIS SOFTWARE WITHOUT SPECIFIC PRIOR WRITTEN PERMISSION.
//
// IN NO EVENT SHALL TEAM MONGOOSE BE HELD LIABLE FOR INCIDENTAL, SPECIAL, INDIRECT,
// INCONSEQUENTIAL, UNBELIEVABLE, EXAGGERATED, VERBOSE, OR TYPICAL DAMAGES INCURRED WHILE USING
// THE ENCLOSED COMPONENTS.
//
// OUR STUFF ALWAYS WORKS - SOMETIMES.

#endregion

using System;
using DiceRoller.Dice;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;

namespace DiceRoller.Tests.Dice
{
    [TestClass()]
    public class DiceTests
    {
        [TestMethod()]
        public void ConstructorShouldRequireDiceParameter()
        {
            try
            {
                new DiceRoller.Dice.Dice(null);
                Assert.Fail("Exception should have been thrown");
            }
            catch (ArgumentNullException exception)
            {
                Assert.AreEqual("Precondition failed: dice != null  dice\r\nParameter name: dice", exception.Message);
#if !SILVERLIGHT
                Assert.AreEqual("dice", exception.ParamName);
#endif
            }
        }

        [TestMethod()]
        public void ConstructorShouldRequireAtLeastOneDie()
        {
            try
            {
                new DiceRoller.Dice.Dice(new Die[0]);
                Assert.Fail("Exception should have been thrown");
            }
            catch (ArgumentException exception)
            {
                Assert.AreEqual("Precondition failed: dice.Count() > 0  dice\r\nParameter name: dice", exception.Message);
#if !SILVERLIGHT
                Assert.AreEqual("dice", exception.ParamName);
#endif
            }
        }

        [TestMethod()]
        public void ConstructorShouldRequireAllDieToNotBeNull()
        {
            try
            {
                new DiceRoller.Dice.Dice(new Die[] { null });
                Assert.Fail("Exception should have been thrown");
            }
            catch (ArgumentException exception)
            {
                Assert.AreEqual("Precondition failed: Contract.ForAll(dice, die => die != null)  dice\r\nParameter name: dice", exception.Message);
#if !SILVERLIGHT
                Assert.AreEqual("dice", exception.ParamName);
#endif
            }
        }

        [TestMethod()]
        public void MinimumValueShouldSumAllDice()
        {
            var mock1 = new Mock<Die>(MockBehavior.Strict, 1, 2);
            mock1.SetupGet(die => die.MinimumValue).Returns(1);

            var mock2 = new Mock<Die>(MockBehavior.Strict, 2, 6);
            mock2.SetupGet(die => die.MinimumValue).Returns(2);

            var dice = new DiceRoller.Dice.Dice(mock1.Object, mock2.Object);
            var result = dice.MinimumValue;

            Assert.AreEqual(3, result);
        }

        [TestMethod()]
        public void MinimumValueShouldAllowOverflowException()
        {
            var mock1 = new Mock<Die>(MockBehavior.Strict, 1, Int32.MaxValue);
            mock1.SetupGet(die => die.MinimumValue).Returns(Int32.MaxValue);

            var mock2 = new Mock<Die>(MockBehavior.Strict, 2, Int32.MaxValue);
            mock2.SetupGet(die => die.MinimumValue).Returns(Int32.MaxValue);

            var dice = new DiceRoller.Dice.Dice(mock1.Object, mock2.Object);

            try
            {
                (dice.MinimumValue).ToString(); // get value
                Assert.Fail("Exception should have been thrown");
            }
            catch (OverflowException exception)
            {
                Assert.AreEqual("Arithmetic operation resulted in an overflow.", exception.Message);
            }
        }

        [TestMethod()]
        public void MaximumValueShouldSumAllDice()
        {
            var mock1 = new Mock<Die>(MockBehavior.Strict, 1, 2);
            mock1.SetupGet(die => die.MinimumValue).Returns(1);
            mock1.SetupGet(die => die.MaximumValue).Returns(2);

            var mock2 = new Mock<Die>(MockBehavior.Strict, 1, 6);
            mock2.SetupGet(die => die.MinimumValue).Returns(1);
            mock2.SetupGet(die => die.MaximumValue).Returns(6);

            var dice = new DiceRoller.Dice.Dice(mock1.Object, mock2.Object);
            var result = dice.MaximumValue;

            Assert.AreEqual(8, result);
        }

        [TestMethod()]
        public void MaximumValueShouldAllowOverflowException()
        {
            var mock1 = new Mock<Die>(MockBehavior.Strict, 1, Int32.MaxValue);
            mock1.SetupGet(die => die.MinimumValue).Returns(1);
            mock1.SetupGet(die => die.MaximumValue).Returns(Int32.MaxValue);

            var mock2 = new Mock<Die>(MockBehavior.Strict, 1, Int32.MaxValue);
            mock2.SetupGet(die => die.MinimumValue).Returns(1);
            mock2.SetupGet(die => die.MaximumValue).Returns(Int32.MaxValue);

            var dice = new DiceRoller.Dice.Dice(mock1.Object, mock2.Object);

            try
            {
                (dice.MaximumValue).ToString(); // get value
                Assert.Fail("Exception should have been thrown");
            }
            catch (OverflowException exception)
            {
                Assert.AreEqual("Arithmetic operation resulted in an overflow.", exception.Message);
            }
        }

        [TestMethod()]
        public void RollShouldSumAllDice()
        {
            var mock1 = new Mock<Die>(MockBehavior.Strict, 1, 2);
            mock1.SetupGet(die => die.MinimumValue).Returns(1);
            mock1.SetupGet(die => die.MaximumValue).Returns(2);
            mock1.Setup(die => die.Roll()).Returns(1);

            var mock2 = new Mock<Die>(MockBehavior.Strict, 1, 6);
            mock2.SetupGet(die => die.MinimumValue).Returns(1);
            mock2.SetupGet(die => die.MaximumValue).Returns(6);
            mock2.Setup(die => die.Roll()).Returns(6);

            var dice = new DiceRoller.Dice.Dice(mock1.Object, mock2.Object);
            var result = dice.Roll();

            Assert.AreEqual(7, result);
        }

        [TestMethod()]
        public void RollShouldAllowOverflowException()
        {
            var mock1 = new Mock<Die>(MockBehavior.Strict, 1, Int32.MaxValue);
            mock1.Setup(die => die.Roll()).Returns(Int32.MaxValue);

            var mock2 = new Mock<Die>(MockBehavior.Strict, 1, Int32.MaxValue);
            mock2.Setup(die => die.Roll()).Returns(Int32.MaxValue);

            var dice = new DiceRoller.Dice.Dice(mock1.Object, mock2.Object);

            try
            {
                dice.Roll();
                Assert.Fail("Exception should have been thrown");
            }
            catch (OverflowException exception)
            {
                Assert.AreEqual("Arithmetic operation resulted in an overflow.", exception.Message);
            }
        }
    }
}
